/*
 * Copyright (c) 2009 DeledImporter
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'Pyro Games' nor the names of its contributors 
 *   may be used to endorse or promote products derived from this software 
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package net.pyrogames.deled.geometry;

import java.util.ArrayList;

import net.pyrogames.deled.math.DXSVector2f;
import net.pyrogames.deled.math.DXSVector3f;

/**
 * Fundamental geometry class. Essentially each poly is a triangle
 * that contains all pertinent data associated with its group of vertices.
 * <p>
 * If the poly is not a triangle, then the <code>DXSImporter</code> needs to
 * triangulate polys. Also, polys in a <code>DXSPrimitive</code> may be broken up
 * based on its materialID.
 * <p>
 * In the DXS file, every vertex associated with a primitive has an ID, and the 
 * internal representation of vertices are aware of their ID's. This is used
 * to assign the normals and set the correct UV texture coordinate
 * to these vertices. This class has convenience methods for easy lookups for 
 * vertices/normals/texture coordinates based on this ID. Especially useful 
 * when we need to break up primitives, triangulate polys, and reuse uv/normal data.
 * 
 * @author Nicholas Woodfield
 *
 */
public class DXSPoly {
	/** Global ID of the material used by this face */
	public int materialID;
	/** Vertices assigned to this face */
	public ArrayList<DXSVector3f> vertices;
	/** Texture coords assigned to this face */
	public ArrayList<DXSVector2f> texVerts;
	/** Normal vectors assigned to this face */
	public ArrayList<DXSVector3f> vertNormals;
	/** Normal vector for the face */
	public DXSVector3f faceNormal;
	
	/**
	 * Default constructor
	 */
	public DXSPoly(){
		materialID = -1;
		vertices = new ArrayList<DXSVector3f>();
		texVerts = new ArrayList<DXSVector2f>();
		vertNormals = new ArrayList<DXSVector3f>(3);
		faceNormal = new DXSVector3f();
	}
	
	/**
	 * Get the corresponding vertex by its ID.
	 * 
	 * @param vid
	 * 		Vertex ID
	 * @return
	 * 		Vertex, otherwise null
	 */
	public DXSVector3f getVertexByID(int vid){
		for(DXSVector3f v : vertices){
			if(v.vid == vid){
				return v;
			}
		}
		return null;
	}
	
	/**
	 * Get the corresponding normal by the ID of the vertex associated with it
	 * 
	 * @param vid
	 * 		Vertex ID
	 * @return
	 * 		Normal vector, otherwise null
	 */
	public DXSVector3f getNormalByID(int vid){
		for(DXSVector3f v : vertNormals){
			if(v.vid == vid){
				return v;
			}
		}
		return null;
	}
	
	/**
	 * Get the corresponding texture coordinate based on the ID of the vertex 
	 * associated with it
	 * 
	 * @param vid
	 * 		Vertex ID
	 * @return
	 * 		Texture coord, otherwise null
	 */
	public DXSVector2f getTexVertByID(int vid){
		for(DXSVector2f v: texVerts){
			if(v.vid == vid){
				return v;
			}
		} return null;
	}
}
